// node 后端服务器
const StuApi = require('./api/StuApi')
const DetailApi = require('./api/DetailApi')
const UserApi = require('./api/UserApi')
const UsApi = require('./api/UsApi')
const MessageApi = require('./api/MessageApi')
const InfoApi = require('./api/infoApi')
const ListApi = require('./api/ListApi')
const fs = require('fs')
const path = require('path')
const bodyParser = require('body-parser')
const express = require('express')
const app = express()
const models = require('./db')
const mysql = require('mysql')
//采用设置所有均可访问的方法解决跨域问题
app.all("*", function (req, res, next) {
    //设置允许跨域的域名，*代表允许任意域名跨域
    res.header("Access-Control-Allow-Origin", "*");
    //允许的header类型
    res.header("Access-Control-Allow-Headers", "content-type");
    //跨域允许的请求方式
    res.header("Access-Control-Allow-Methods", "DELETE,PUT,POST,GET,OPTIONS");
    if (req.method.toLowerCase() == 'options')
        res.send(200); //让options尝试请求快速结束
    else
        next();
})
let conn = mysql.createConnection(models.mysql_config)
var i =1
setInterval(function () {
     i += 1
    conn.query('SELECT 1');
    console.log(i)
    conn.close;
  }, 10000)
app.use(bodyParser.json()) // 以json格式返回出去
app.use(bodyParser.urlencoded({ extended: false }))
// 后端api路由

app.use('/api/Stu', StuApi)
app.use('/api/detail', DetailApi)
app.use('/api/userInfo', UserApi)
app.use('/api/message', MessageApi)
app.use('/api/user', UsApi)
app.use('/api/info', InfoApi)
app.use('/api/list', ListApi)

// 监听端口
app.listen(3000)
console.log('服务器连接成功')
console.log('success listen at port:3000......')